package org.budo.dubbo.protocol.http.dispatcher.cometd;

import java.io.InputStream;

import org.budo.dubbo.protocol.http.invocation.adapter.ProtocolResponse;
import org.cometd.bayeux.server.ServerMessage;
import org.cometd.bayeux.server.ServerSession;
import org.cometd.server.BayeuxServerImpl;

/**
 * @author li
 */
public class CometdProtocolResponse implements ProtocolResponse {
    private BayeuxServerImpl bayeuxServer;

    private ServerSession serverSession;

    public CometdProtocolResponse(BayeuxServerImpl bayeuxServer, ServerSession serverSession) {
        this.bayeuxServer = bayeuxServer;
        this.serverSession = serverSession;
    }

    @Override
    public void addHeader(String name, String value) {
        throw new RuntimeException();
    }

    /**
     * 对单个session发消息
     */
    @Override
    public void write(InputStream inputStream) {
        ServerMessage.Mutable forwardMutable = bayeuxServer.newMessage();
        forwardMutable.setChannel(BudoBayeuxServerFactoryBean.RESPONSE_CHANNEL);
        forwardMutable.setData(inputStream);
        serverSession.deliver(serverSession, forwardMutable);
    }
}